﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RedBit.UserVoice.Services;
using RedBit.UserVoice.OAuth;
using RedBit.UserVoice.Model;
using RedBit.UserVoice.Helpers;

namespace SampleApplication
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!UVUserService.IsLoggedIn)
            {
                puLogin.IsOpen = true;
            }
            else
            {
                UVUserHelper.Initialize(user =>
                {
                    LoadForm(user.Forums.First());
                });
            }
        }

        private void cbRegister_Click(object sender, RoutedEventArgs e)
        {
            var user = new OAuthUser
            {
                Guid = Guid.NewGuid().ToString(),
                Name = tbName.Text,
                Email = tbEmail.Text,
            };
            //TODO: Add error handling
            UVUserService.CreateUser(user, u => HandleUserResponse(u));
        }

        private void HandleUserResponse(UVResponse<OAuthUser> u)
        {
            //TODO: Add error handling
            if (u.Error == null)
            {
                UVUserHelper.Initialize(user =>
                    {
                        LoadForm(user.Forums.First());
                    });
            }
                
        }

        private void LoadForm(UVForum uVForum)
        {
            UVSuggestionService.GetSuggestionsForForum(uVForum.Id, suggestions => DisplaySuggestions(suggestions));
        }

        private void DisplaySuggestions(UVResponse<ICollection<UVSuggestion>> response)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    lbSuggestions.ItemsSource = response.Model;
                    puLogin.IsOpen = false;
                });
        }

        private void cbAdd_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddSuggestionPage.xaml", UriKind.Relative));
        }


        private const string DETAILS_URL = "/DetailsPage.xaml?suggestionId={0}&forumId={1}";

        private void lbSuggestions_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbSuggestions.SelectedIndex > -1)
            {
                var suggestion = lbSuggestions.SelectedItem as UVSuggestion;

                NavigationService.Navigate(new Uri(string.Format(DETAILS_URL, suggestion.Id, suggestion.ForumId), UriKind.Relative));

                lbSuggestions.SelectedIndex = -1;
            }
        }
    }
}